import {BActor} from "./BActor.ts";
import {ACTOR_TYPE} from "../interface.ts";
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkPoints from '@kitware/vtk.js/Common/Core/Points';
import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData';
import vtkCellArray from "@kitware/vtk.js/Common/Core/CellArray";
import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";

export class LinesActor extends BActor {
    createdBy: string = "default";
    instance = vtkActor.newInstance();
    readonly type: ACTOR_TYPE = ACTOR_TYPE.GRAPHIC_ACTOR;
    points: vtkPoints = vtkPoints.newInstance();
    lines: vtkCellArray = vtkCellArray.newInstance();
    mapper = vtkMapper.newInstance();
    polyData = vtkPolyData.newInstance();

    constructor(points: number []) {
        super();
        this.points.setData(points);
        this.createCells(points.length / 6);
        this.polyData.setPoints(this.points);
        this.polyData.setLines(this.lines);
        this.mapper.setInputData(this.polyData);
        this.instance.setMapper(this.mapper);
        this.disableLight();
        this.setColor(255,0,0);
        this.setWidth(2);
    }

    /**
     * 设置颜色
     * @param r - 255
     * @param g - 255
     * @param b - 255
     */
    setColor(r:number,g:number,b:number) {
        this.instance.getProperty().setColor(r/255, g/255, b/255);
    }

    setWidth(width:number) {
        this.instance.getProperty().setLineWidth(width);
    }

    /**
     * 线条数
     * @param n
     */
    createCells(n: number) {
        for (let i = 0; i < n; i++) {
            this.lines.insertNextCell([i * 2, i * 2 + 1]);
        }
    }
}